<template>
  <BaseTable
    ref="baseTableRef"
    :apiUrl="getImageTypeList"
    :columns="standardImageColumns"
    :rowId="id"
    :otherQuery="{ templateType }"
    placeholder="请输入镜像名称"
    :isShowAddBtn="false"
  >
    <template #operatBtns>
      <ga-button
        type="primary"
        @click="
          tab == 'privateImage' ? $refs.addPrivateRef.open() : $refs.addOrEditRef.open()
        "
      >
        创建
      </ga-button>
    </template>
    <template #action="{ record }">
      <ga-link status="danger" @click="handleDelete([record])">删除</ga-link>
    </template>
  </BaseTable>
  <addOrEdit
    ref="addOrEditRef"
    :templateType="templateType"
    @fetchData="$refs.baseTableRef.fetchData()"
  />
  <addPrivate
    ref="addPrivateRef"
    :templateType="templateType"
    @fetchData="$refs.baseTableRef.fetchData()"
  />
</template>

<script setup>
import { ref, onMounted } from "vue";
import { standardImageColumns } from "../../columns";
import { deleteTemplate, getImageTypeList } from "@/api/operatingSystem";
import addOrEdit from "./addOrEdit.vue";
import addPrivate from "../privateImage/addPrivate.vue";
import { useRoute } from "vue-router";
import { warningModal } from "@/hooks/useWarningModal";

defineProps({
  templateType: {
    type: String,
    default: "public_standard",
  },
});
const route = useRoute();
const { id } = route.params;
const { tab } = route.query;
// 删除方法
const baseTableRef = ref();
const handleDelete = async (records) => {
  const ids = records.map((item) => item.id || item);
  warningModal(`确定要删除数据吗`, async () => {
    try {
      const { code } = deleteTemplate(id, ids);
      if (code === 20000) Message.success("操作成功");
      baseTableRef.value.fetchData();
    } catch (err) {
      console.log(err);
    }
  });
};
onMounted(() => {});
</script>

<style lang="scss" scoped></style>
